//
//  CollectViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/20.
//

import UIKit

class CollectViewController: BaseViewController {
    var dataSource:[AIMessage] = []
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = UITableView.automaticDimension
        tableView.register(cellWithClass: CollectTableViewCell.self)
        tableView.separatorStyle = .none
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
//        tableView.contentInsetAdjustmentBehavior = .never
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        return tableView
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.editNav(isCurrent: true)
        self.reloadData()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.editNav(isCurrent: false)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(hexString: "#F3F2F7")
        self.navigationItem.title = "收藏"
        self.view.addSubview(tableView)
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.safeTop)
            make.bottom.equalTo(self.safeBottom)
            make.leading.trailing.equalToSuperview()
        }
    }
    
    func reloadData(){
        self.dataSource = SqlManager.shared.filter(from: AIMessage.self, where: AIMessage.Properties.collect == true) ?? []
        self.tableView.reloadData()
    }
   
}
extension CollectViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = self.dataSource[indexPath.item]
        let cell = tableView.dequeueReusableCell(withClass: CollectTableViewCell.self, for: indexPath)
        cell.contentLb.text = model.message ?? ""
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = CollectDetailsViewController()
        vc.model = self.dataSource[indexPath.item]
        self.navigationController?.pushViewController(vc)
    }
    
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .destructive, title: nil) { (action, sourceView, completionHandler) in
            var model = self.dataSource[indexPath.item]
            model.collect = false
            SqlManager.shared.update(on: [AIMessage.Properties.collect], with: model, where: AIMessage.Properties.sort == model.sort ?? 0)
            self.reloadData()
            completionHandler(true)
        }

        // 设置自定义图片
        deleteAction.image = UIGraphicsImageRenderer(size: CGSize(width: 42, height: 42)).image { _ in
            UIImage(named: "icon_collect_delete")?.draw(in: CGRect(x: 0, y: 0, width: 42, height: 42))
        }
        
        // 创建一个透明背景的 UIImage
        let renderer = UIGraphicsImageRenderer(size: CGSize(width: 1, height: 1))
        let clearImage = renderer.image { context in
            UIColor.clear.setFill()
            context.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
        }
        
        // 设置背景颜色
        deleteAction.backgroundColor = UIColor(patternImage: clearImage)

        let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
        // 禁止滑动整个单元格就执行删除
        configuration.performsFirstActionWithFullSwipe = false

        return configuration
    }


 
    
    
}
extension CollectViewController: DZNEmptyDataSetDelegate, DZNEmptyDataSetSource {
    open func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        UIImage(named: "icon_nodata")
    }
    
    open func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let text = "暂无数据"
        let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.pingFangSCRegular(ofSize: 15), .foregroundColor: Theme.color.text_d3]
        return NSAttributedString(string: text, attributes: attributes)
    }
    
    //    func verticalOffset(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
    //        return -navHig()
    //    }
    
    func emptyDataSet(_ scrollView: UIScrollView!, didTap view: UIView!) {
        //        self.getDataSource()
        //        self.navigationController?.pushViewController(MusicImportViewController())
    
    }
    
}
